package com.supermap.business.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.common.YieldService;
import com.supermap.dubbo.api.common.entity.Crop;
import com.supermap.dubbo.api.common.entity.Yield;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:产量的接口层
 * @Date:18/8/22 下午2:45
 */
@Controller
@RequestMapping("YieldController")
public class YieldController {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(YieldController.class);

    @Reference(timeout = 5000)
    private YieldService yieldService;


    /**
     *通过作物和次数获取产量
     * @param cropCode
     * @param
     * @return
     */
    @RequestMapping("getYieldsByCropAndTime")
    @ResponseBody
    public JSONUtil getYieldsByCropAndTime(String cropCode){
        return yieldService.getYieldsByCropAndTime(cropCode);
    }

    /**
     * 批量更新产量信息
     * @param request
     * @return
     */
    @RequestMapping("updateYieldsInfo")
    @ResponseBody
    public JSONUtil updateYieldsInfo(HttpServletRequest request){
        JSONArray jsonArray = (JSONArray) JSONArray.parse(request.getParameter("yields"));
        List<Yield> yields = new ArrayList<>();
        for (int i=0;i<jsonArray.size();i++) {
            Yield yield = new Yield();
            yield.setId(Integer.parseInt((((JSONObject)jsonArray.get(i)).get("id")).toString()));
            yield.setYield(Double.parseDouble((((JSONObject)jsonArray.get(i)).get("yield")).toString()));
            yields.add(yield);
        }
        return yieldService.updateYieldsInfo(yields);
    }

    /**
     * 增加产量信息
     * @param yield
     * @return
     */
    @RequestMapping("addYieldInfo")
    @ResponseBody
    public JSONUtil addYieldInfo(Yield yield){
        return yieldService.addYieldInfo(yield);
    }

}
